import app
import logging


class LoginApi:
    # 获取图片验证码
    get_img_code_url = app.BASE_URL + "/common/public/verifycode1/{}"
    # 获取短信验证码
    get_sms_code_url = app.BASE_URL + "/member/public/sendSms"
    # 注册
    reg_url = app.BASE_URL + "/member/public/reg"
    # 登录
    login_url = app.BASE_URL + "/member/public/login"
    # 是否登录
    is_login_url = app.BASE_URL + "/member/public/islogin"

    # 获取图片验证码
    @classmethod
    def get_img_code(cls, session, r):
        logging.info(f"param r={r}")
        url = cls.get_img_code_url.format(r)
        logging.info(f"url={url}")
        return session.get(url)

    # 获取短信验证码
    @classmethod
    def get_sms_code(cls, session, body):
        return session.post(cls.get_sms_code_url, data=body)

    # 注册
    @classmethod
    def reg(cls, session, phone, password, verifycode, phone_code, dy_server, invite_phone):
        data = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code,
            "dy_server": dy_server,
        }
        if invite_phone is not None:
            data["invite_phone"] = invite_phone
        print("reg data==", data)
        return session.post(cls.reg_url, data=data)

    # 注册2
    @classmethod
    def reg2(cls, session, body):
        print("reg2 body==", body)
        return session.post(cls.reg_url, data=body)

    # 登录
    @classmethod
    def login(cls, session, body):
        print("login body==", body)
        return session.post(cls.login_url, data=body)

    # 是否登录
    @classmethod
    def is_login(cls, session):
        return session.post(cls.is_login_url)

    # def hello(self):
    #     pass
    #
    # @staticmethod
    # def test():
    #     pass

# class LoginApi2:
#     def __init__(self):
#         self.get_img_code_url = app.BASE_URL + "/common/public/verifycode1/{}"
#
#     def get_img_code(self, r):
#         url = self.get_img_code_url.format(r)
#         return requests.get(url)
#
# LoginApi2().get_img_code(111)


# Python中方法的类型：
# 实例方法：
#   - 定义的普通方法，第一个参数是self
#   - 必须通过类的对象才能调用：LoginApi().hello()
# 类方法
#   - 在方法上加上@classmethod装饰器，第一个参数是cls
#   - 调用方法：LoginApi.get_img_code("123")
# 静态方法
#   - 在方法上加上@staticmethod装饰器，默认没有参数
#   - 调用方法：LoginApi.test()

# class Student:
#     def __init__(self, name):
#         self.name = name
#
# zhangsan = Student("张三")
# lisi = Student("李四")
